import { ref, computed } from 'vue'
import { defineStore } from 'pinia'


export const useMeunItemStore = defineStore('menuItem', () => {
const menuItemId = ref<any>([])
const menuItem = ref<any>([])
  

//   const doubleCount = computed(() => count.value * 2)

  const doubleCount = computed(() =>{
    // console.log(localStorage.getItem('menuItem'));
    let lg = localStorage.getItem('menuItem')
    console.log(lg );
    let arr = null
    if(lg){
      arr =  JSON.parse(lg)
    }
    
    
    return arr
   
    
  })

  function getMeunlist() {
    console.log(localStorage.getItem('menuItem'));
    let lg:any = localStorage.getItem('menuItem')
    console.log(lg );
    // let arr:any = null
    // arr =  JSON.parse(lg)
    let arr = null
    if(lg){
      arr =  JSON.parse(lg)
    }
    console.log(arr );
  }


  function setMeunlist(item:any) {
    // console.log(item,item[0]);
    if (item) {
      item.forEach((e:any)=> {
        // console.log(e);
        let index = menuItemId.value.indexOf(e.id)
        if (index == -1) {
          menuItemId.value.push(index)
          menuItem.value = item
        }
      });
    }
    
    
    
    
  }
//   function findValueByKey(obj:any, keyToFind:any) {
//     for (let key in obj) {
//       if (key === keyToFind) {
//         return obj[key];
//       }
//     }
//     return null; // 如果找不到键，返回null
//   }
  return { menuItem,menuItemId,doubleCount, setMeunlist,getMeunlist }
},{persist: true,})
